// 将第一个数组arr过滤成第二个数组形式
let arr = [
  {
    key: 1,
    index: 1,
    name: "阿里巴巴",
    createTime: "2023-02-03 11:11:21",
    status: ["正常"],
    children: [
      {
        key: 11,
        index: 2,
        name: "深圳分公司",
        createTime: "2023-02-03 11:11:21",
        status: ["正常"],
        children: [
          {
            key: 111,
            index: 1,
            name: "研发部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
          {
            key: 112,
            index: 2,
            name: "市场部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
          {
            key: 113,
            index: 3,
            name: "测试部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
          {
            key: 114,
            index: 4,
            name: "财务部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
          {
            key: 115,
            index: 5,
            name: "运维部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
        ],
      },
      {
        key: 12,
        index: 2,
        name: "北京分公司",
        createTime: "2023-02-03 11:11:21",
        status: ["正常"],
        children: [
          {
            key: 121,
            index: 1,
            name: "市场部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
          {
            key: 122,
            index: 2,
            name: "财务部门",
            createTime: "2023-02-03 11:11:21",
            status: ["正常"],
          },
        ],
      },
    ],
  },
]

let brr = [
  {
    title: "阿里巴巴",
    children: [
      {
        title: "深圳分公司",
        children: [
          {
            title: "研发部门",
          },
          {
            title: "市场部门",
          },
          {
            title: "测试部门",
          },
          {
            title: "财务部门",
          },
          {
            title: "运维部门",
          },
        ],
      },
      {
        title: "北京分公司",
        children: [
          {
            title: "市场部门",
          },

          {
            title: "财务部门",
          },
        ],
      },
    ],
  },
]

const filterArr = (arr)=>{
    // 判断arr类型,不是数组直接返回
    if(!arr || !Array.isArray(arr)){
        return
    }
    let newArr = []  // 存储转换后的数组
    // 遍历原数组，有children递归过滤
    arr.forEach(item=>{
        let children
        if(item.children && Array.isArray(item.children)){
            children = filterArr(item.children)
        }

        // 构造满足条件新元素
        const obj = {
            title:item.name,
            children
        }
        //删除多余children
        if(!obj.children){
            delete obj.children
        }
        newArr.push(obj) // 存储新元素
    })
    return newArr
}

console.log(filterArr(arr))